package com.uusee.crawler.pageprocessor.video;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.oro.text.regex.MatchResult;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.PatternMatcherInput;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;

import com.uusee.crawler.framework.Processor;
import com.uusee.crawler.model.CrawlStatusCodes;
import com.uusee.crawler.model.CrawlURI;
import com.uusee.crawler.util.PageProcessorUtils;
import com.uusee.crawler.util.DomainUtils;
import com.uusee.shipshape.sp.model.Ugc;

public class UserVideoPageProcessor extends Processor {
	private static final String LI_REG = "<li class=\"mt6\">[\\s]*?([\\S].*?)<.*?>";
	private static final String A_REG = "<a.*?>[\\s]*?([\\S].*?)</a>";
	private static final String P_REG = "<\\s*?p.*?>(.*?)<\\s*?/p\\s*?>";
	private static Log log = LogFactory.getLog(UserVideoPageProcessor.class);
	private Properties pageProcessRegex;
	
	public void setPageProcessRegex(Properties pageProcessRegex) {
		this.pageProcessRegex = pageProcessRegex;
	}
	
	private boolean canProcess(CrawlURI crawlURI) {
		int crawlStatus = crawlURI.getCrawlStatus();
		if (crawlStatus == CrawlStatusCodes.FETCH_SUCCESS) {
			return true;
		} else if (crawlStatus == CrawlStatusCodes.FETCH_SC_NOT_OK) {
			crawlURI.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_INVALID);
			return false;
		} else {
			return false;
		}
	}

	public void innerProcess(CrawlURI crawlURI) {
		if (!canProcess(crawlURI)) {
			return;
		}
		List<CrawlURI> playCrawlURIList = new ArrayList<CrawlURI>();
		
		try {
			Date crawlDate = new Date();
			String videoSite = crawlURI.getSourceSite();
			String charset = crawlURI.getCharset();
			String crawlUrl = crawlURI.getCrawlUrl();
			String crawlResult = crawlURI.getCrawlResult();
			
			//分析用户信息
			String uploadUserid = "";
			String uploadUsername = "";
			String uploadUserblog = "";
			if (pageProcessRegex.containsKey("uploadUserid")) {
				uploadUserid = PageProcessorUtils.getValue(pageProcessRegex.getProperty("uploadUserid"), crawlResult);
			}
			if (pageProcessRegex.containsKey("uploadUsername")) {
				uploadUsername = PageProcessorUtils.getValue(pageProcessRegex.getProperty("uploadUsername"), crawlResult);
			}
			if (pageProcessRegex.containsKey("uploadUserblog")) {
				uploadUserblog = PageProcessorUtils.getValue(pageProcessRegex.getProperty("uploadUserblog"), crawlResult);
			}
			if (StringUtils.isNotEmpty(uploadUserblog) && !uploadUserblog.startsWith("http://")) {
				String domain = PageProcessorUtils.getValue("http://([^/]*?)/", crawlUrl);
				uploadUserblog = "http://" + domain + uploadUserblog;
			}
			
			//过滤
			String videoListText = crawlResult;
			if (pageProcessRegex.containsKey("filters")) {
				String filters = pageProcessRegex.getProperty("filters");
				String[] filterArray = filters.split("::");
				for (String filter:filterArray) {
					videoListText = videoListText.replaceAll(filter, "");
				}
			}
			
			//取得结果
			String videoListReg = pageProcessRegex.getProperty("modelList");
			
			videoListText = PageProcessorUtils.getValue(videoListReg, videoListText);

			PatternCompiler compiler = new Perl5Compiler();
			String videoRegex = pageProcessRegex.getProperty("model");
			Pattern pattern = compiler.compile(videoRegex, Perl5Compiler.CASE_INSENSITIVE_MASK);
			PatternMatcher matcher = new Perl5Matcher();
			PatternMatcherInput input = new PatternMatcherInput(videoListText);
			while (matcher.contains(input, pattern)) {
				try {
					MatchResult m = matcher.getMatch();
					String videoText = m.group(1);
					
					Ugc newVideo = new Ugc();
					Set<Object> keySet = pageProcessRegex.keySet();
					for (Object key:keySet) {
						String name = (String)key;
						if (pageProcessRegex.containsKey(name)) {
							String value = PageProcessorUtils.getValue(pageProcessRegex.getProperty(name), videoText);
							if (StringUtils.isNotEmpty(value)&&PropertyUtils.isWriteable(newVideo, name)) {
								if (PageProcessorUtils.isMatch(A_REG, value)) {
									value = PageProcessorUtils.getValues(A_REG, value);
								}
								if (PageProcessorUtils.isMatch(LI_REG, value)) {
									value = PageProcessorUtils.getValues(LI_REG, value);
								}
								if (PageProcessorUtils.isMatch(P_REG, value)) {
									value = value.replaceAll("<\\s*/p\\s*>\\s*?<\\s*p[^<]*?>", "::P_FLAG");
									value = value.replaceAll("<BR>", "::P_FLAG");
									value = value.replaceAll("<[^<]*?>", "");
									value = value.replaceAll("::P_FLAG", "<br/>");
								}else{
									value = value.replaceAll("<[^<]*?>", "");
								}
								value = StringUtils.trim(value);
								value = PageProcessorUtils.unicodeToChinese(value);
								if (name.equals("totalPageView")||name.equals("totalComment")||name.equals("totalFav")) {
									value = value.replaceAll("\\D", "");
									int iValue = 0;
									try {
										iValue = Integer.valueOf(value);
									}catch(Exception e){}
									PropertyUtils.setProperty(newVideo, name, iValue);
								}else if (name.equals("length")) {
									Integer length = PageProcessorUtils.getLength(value);
									PropertyUtils.setProperty(newVideo, name, length);
								} else {
									PropertyUtils.setProperty(newVideo, name, value);
								}
								
							}
						}
					}
					String title = newVideo.getTitle();
					String vid = newVideo.getVid();
					String evid = newVideo.getEvid();
					String playUrl = newVideo.getPlayUrl();
					String shareUrl = newVideo.getShareUrl();
					
					//土豆：处理VID
					if (StringUtils.isEmpty(vid)) {	
						if (pageProcessRegex.containsKey("vidValue")) {
							String vidValue = pageProcessRegex.getProperty("vidValue");
							String result1 = PageProcessorUtils.getValue(vidValue, videoText, 1);
							String result2 = PageProcessorUtils.getValue(vidValue, videoText, 2);
							String result3 = PageProcessorUtils.getValue(vidValue, videoText, 3);
							
							String result = result1+result2+result3;
							while(result.startsWith("0")){
								result = StringUtils.removeStart(result, "0");
							}
							if (StringUtils.isNumeric(result)) {
								vid=result;
							}
						}
					}
						
					// 处理地址
					if (StringUtils.isEmpty(playUrl)) {	
						if (pageProcessRegex.containsKey("playUrlRule")) {
							String playUrlRule = pageProcessRegex.getProperty("playUrlRule");
							String result = PageProcessorUtils.replaceFlag(playUrlRule,"::VID",vid);
							result = PageProcessorUtils.replaceFlag(result,"::EVID",evid);
							result = PageProcessorUtils.replaceFlag(result,"::UPLOAD_USER_ID",uploadUserid);
							if (!result.equals(playUrlRule)) {
								playUrl = result;
							}
						}
					}

					if (StringUtils.isEmpty(shareUrl)) {
						if (pageProcessRegex.containsKey("shareUrlRule")) {
							String shareUrlRule = pageProcessRegex.getProperty("shareUrlRule");
							String result = PageProcessorUtils.replaceFlag(shareUrlRule,"::VID",vid);
							result = PageProcessorUtils.replaceFlag(result,"::EVID",evid);
							result = PageProcessorUtils.replaceFlag(result,"::UPLOAD_USER_ID",uploadUserid);
							if (!result.equals(shareUrlRule)) {
								shareUrl = result;
							}
						}
					}
					newVideo.setVid(vid);
					newVideo.setPlayUrl(playUrl);
					newVideo.setShareUrl(shareUrl);
					
					// 标题和播放地址不能为空
					if (StringUtils.isEmpty(title) || StringUtils.isEmpty(playUrl)) {
						continue;
					}
					
					String videoDomain = DomainUtils.getDomain(playUrl);
					String sourceSite = PageProcessorUtils.getValue("http://([^/]*?)/", playUrl);

					newVideo.setDomain(videoDomain);
					newVideo.setVideoSite(videoSite);
					newVideo.setSourceSite(sourceSite);
					newVideo.setUploadUserid(uploadUserid);
					newVideo.setUploadUsername(uploadUsername);
					newVideo.setUploadUserblog(uploadUserblog);
					newVideo.setStatus("1");
					newVideo.setCrawlUser("uusee_crawler");
					newVideo.setCrawlDate(crawlDate);
					newVideo.setUpdateUser("uusee_crawler");
					newVideo.setUpdateDate(crawlDate);
					
					CrawlURI playCrawlURI = new CrawlURI();
					playCrawlURI.setSourceSite(videoSite);
					playCrawlURI.setCharset(charset);
					playCrawlURI.setCrawlStatus(CrawlStatusCodes.DEFAULT_STATUS);
					playCrawlURI.setCrawlUrl(newVideo.getPlayUrl());
					playCrawlURI.setModel(newVideo);
					
					playCrawlURIList.add(playCrawlURI);
				} catch (Exception e) {
					e.printStackTrace();
					log.error(e);
				}
			}
			
			if (playCrawlURIList.size()>0) {
				crawlURI.setModel(playCrawlURIList);
				crawlURI.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_SUCCESS);
			}else{
				crawlURI.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_INVALID);
				log.info("用户视频页面无效："+uploadUserid);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.equals(e);
			crawlURI.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_EXCEPTION);
		}
	}
}



